package com.wm.wmcrm.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.wmcrm.common.constant.SystemConstant;
import com.wm.wmcrm.common.result.CurrentUsername;
import com.wm.wmcrm.common.util.PageUtils;
import com.wm.wmcrm.sys.entity.SysUser;
import com.wm.wmcrm.sys.mapper.SysUserMapper;
import com.wm.wmcrm.sys.model.SysUserQueryDTO;
import com.wm.wmcrm.sys.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 系统用户 服务实现类
 * </p>
 *
 * @author wm
 * @since 2023-02-11
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    SysUserMapper sysUserMapper;
    @Override
    public SysUser queryByUserName(java.lang.String username) {
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(username),"username",username);
        wrapper.eq("status", SystemConstant.USER_STATUS_NORMAL);
        return this.baseMapper.selectOne(wrapper);
    }

    @Override
    public PageUtils queryPage(SysUserQueryDTO userQueryDTO) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(userQueryDTO.getUsername()),SysUser::getUsername,userQueryDTO.getUsername());
        Page<SysUser> page = this.page(userQueryDTO.page(), wrapper);
        return new PageUtils(page);
    }

    @Override
    public boolean updateStatusById(int userId,int status) {
        return sysUserMapper.updateStatusById(userId,status);
    }

    @Override
    public boolean deleteOne(java.lang.String userId) {
        return this.removeById(userId);
    }

    @Override
    public boolean checkUsernameExist(java.lang.String username) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<SysUser>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(username),SysUser::getUsername,username);
        Integer count = this.baseMapper.selectCount(lambdaQueryWrapper);
        return count>0;
    }

    /**
     * 添加用户
     * @param sysUser
     * @return
     */
    @Override
    public boolean saveUser(SysUser sysUser) {
        sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
        sysUser.setCreateTime(LocalDateTime.now());
        sysUser.setStatus(1);
        String currentUsername = CurrentUsername.getCurrentUsername();
        SysUser CurrentsysUser = this.baseMapper.selectOne(new LambdaQueryWrapper<SysUser>()
                .eq(StringUtils.isNotBlank(currentUsername), SysUser::getUsername, currentUsername));
        sysUser.setCreateUserId(CurrentsysUser.getUserId());
        return this.saveOrUpdate(sysUser);
    }

    @Override
    public boolean checkExist(java.lang.String key, java.lang.String value) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.eq(StringUtils.isNotBlank(value),key,value);
        Integer count = this.baseMapper.selectCount(queryWrapper);
        System.out.println("count"+count);
        return count>0;
    }
    @Override
    public boolean updateOne(SysUser sysUser) {
        return this.updateById(sysUser);
    }

    @Override
    public int deleteBatch(List<String> userIdList) {
        return this.baseMapper.deleteBatchIds(userIdList);
    }


}
